<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料编码" prop="materialNum">
        <el-input
          v-model="queryParams.materialNum"
          placeholder="请输入物料编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物资名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="规格" prop="standards">-->
        <!--<el-input-->
          <!--v-model="queryParams.standards"-->
          <!--placeholder="请输入规格"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="用途" prop="purpose">-->
        <!--<el-input-->
          <!--v-model="queryParams.purpose"-->
          <!--placeholder="请输入用途"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="类别" prop="category">
        <el-cascader v-model="queryParams.category" clearable filterable :options="sysBmTypeList" :props="propsBm" @change="treeSelectFn1"></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['park:maMaterial:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['park:maMaterial:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:maMaterial:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park:maMaterial:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="$router.push('/dict/type/data/88')"
          v-hasPermi="['park:maMaterial:add']"
        >单位</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maMaterialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="Id" align="center" prop="id" />-->
      <el-table-column label="物资名称" align="center" prop="materialName" />
      <el-table-column label="物料编码" align="center" prop="materialNum" />
      <el-table-column label="类别" align="center" prop="categoryName" />
      <el-table-column label="规格" align="center" prop="standards" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="用途" align="center" prop="purpose" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['park:maMaterial:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:maMaterial:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="物料编码" prop="materialNum">-->
          <!--<el-input v-model="form.materialNum" placeholder="请输入物料编码" />-->
        <!--</el-form-item>-->
        <el-form-item label="物资名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物资名称" :disabled="this.form.id != null"/>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <!--<el-input v-model="form.category" placeholder="请输入类别" />-->
          <!--<el-cascader v-model="form.category" :disabled="false" :options="sysBmTypeList" :props="propsBm" :show-all-levels="false" @change="treeSelectFn"></el-cascader>-->
          <el-cascader v-model="form.category" filterable :options="sysBmTypeList" :props="propsBm" :show-all-levels="false" @change="treeSelectFn"></el-cascader>
        </el-form-item>
        <el-form-item label="规格" prop="standards">
          <el-input v-model="form.standards" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择单位">
            <el-option
              v-for="item in unitTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="用途" prop="purpose">
          <el-input v-model="form.purpose" placeholder="请输入用途" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaMaterial, getMaMaterial, delMaMaterial, addMaMaterial, updateMaMaterial } from "@/api/smartParks/property/maMaterial";
import { listData } from "@/api/system/dict/data"; //字典接口	  import { listData } from "@/api/system/dict/data"; //字典接口
import { listSysBmGoods } from"@/api/system/sysBmGoods.js";
import * as request from '@/utils/request';

export default {
  name: "MaMaterial",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物资表格数据
      maMaterialList: [],
      unitTypeList:[],
      sysBmTypeList:[],
      propsBm:{
        label:'name',
        value:'bmId',
        children:'children'
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialNum: null,
        materialName: null,
        standards: null,
        unit: null,
        unitPrice: null,
        purpose: null,
        tenantId: null,
        category: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialName: [
          { required: true, message: "物资名称不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "类别不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    listData({ dictType: "unit_type" }).then((response) => {
      this.unitTypeList = response.rows;
    });
    this.getbmTree();
  },
  methods: {
    treeSelectFn1(){
      this.queryParams.category = this.queryParams.category[1]
    },
    treeSelectFn(node){
      return
      if (node.bmId==0){
        this.form.lv=1
        this.form.parentId=0
      }else {
        this.form.parentId=node.bmId
        this.form.lv=node.lv+1
      }
    },  /** 查询编码配置下拉树结构 */
    getbmTree() {
      listSysBmGoods().then(response => {
        this.sysBmTypeList=(this.toTree(response.data))
      });
    },
    toTree(data){
      let result = []
      if (!Array.isArray(data)) {
        return result
      }
      data.forEach(item => {
        delete item.children;
      });
      let map = {};
      data.forEach(item => {
        map[item.bmId] = item;
      });
      data.forEach(item => {
        let parent = map[item.parentId];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      result.forEach(function(item,index,arr){
        if(item.children != null){
          item.children.forEach(itm=>{
            if(itm.children != null){
              delete itm.children;
            }
          })
        }
      })

      return result;
    },
    /** 查询物资列表 */
    getList() {
      this.loading = true;
      listMaMaterial(this.queryParams).then(response => {
        // response.rows.forEach((item) => {
        //   this.unitTypeList.forEach((itemType) => {
        //     if (itemType.dictValue == item.unit) {
        //       item.unitName = itemType.dictLabel;
        //     }
        //   });
        // });
        this.maMaterialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialNum: null,
        materialName: null,
        standards: null,
        unit: null,
        unitPrice: null,
        purpose: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null,
        category: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaMaterial(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物资";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.category = this.form.category[1];
          if(this.form.standards == null){
            this.form.standards = "";
          }
          if (this.form.id != null) {
            updateMaMaterial(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaMaterial(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除物资编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMaMaterial(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      request.downloadCheckMeter1('park/maMaterial/export', {
        ...this.queryParams
      }, `park_maMaterial.xlsx`)
    }
  }
};
</script>
